package com.nit.controller;

import com.alibaba.fastjson.JSONObject;
import com.nit.dto.GoodsDTO;
import com.nit.dto.utils.OpResultDTO;
import com.nit.dto.utils.TableRequestDTO;
import com.nit.dto.utils.TableResponseDTO;
import com.nit.service.GoodsService;
import com.nit.utils.HttpRequestReader;
import com.nit.utils.SystemConstant;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;
    private final Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableResponseDTO query(@RequestBody TableRequestDTO tableRequestDTO) {
        TableResponseDTO tableResponseDTO = new TableResponseDTO();
        try {
            tableResponseDTO = goodsService.query(tableRequestDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableResponseDTO;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody GoodsDTO goodsDTO) {
        OpResultDTO opResultDTO = new OpResultDTO(-1L, SystemConstant.OP_FAILURE_OBJ);
        try {
            opResultDTO.setLongResult(goodsService.add(goodsDTO));
        } catch (Exception e) {
            logger.error(e.toString());
        }
        if (opResultDTO.getLongResult() != -1L) {
            opResultDTO.setObjResult(SystemConstant.OP_SUCCESS_OBJ);
        }
        return opResultDTO;
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResultDTO = new OpResultDTO(-1L, SystemConstant.OP_FAILURE_OBJ);
        try {
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            opResultDTO.setLongResult(goodsService.remove(jsonObject.getString("goodsIds")));
        } catch (Exception e) {
            logger.error(e.toString());
        }
        if (opResultDTO.getLongResult() != -1L) {
            opResultDTO.setObjResult(SystemConstant.OP_SUCCESS_OBJ);
        }
        return opResultDTO;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody GoodsDTO goodsDTO) {
        OpResultDTO opResultDTO = new OpResultDTO(-1L, SystemConstant.OP_FAILURE_OBJ);
        try {
            opResultDTO.setLongResult(goodsService.update(goodsDTO));
        } catch (Exception e) {
            logger.error(e.toString());
        }
        if (opResultDTO.getLongResult() != -1L) {
            opResultDTO.setObjResult(SystemConstant.OP_SUCCESS_OBJ);
        }
        return opResultDTO;
    }
}
